﻿/*----------------------------------------------------------------
//  <copyright file="UserFunctionController.cs" company="MicroCloud@151504200868">
//      Copyright © 2020-2024 MicroCloud Corporation, All rights reserved.
//  </copyright>
//  <site>https://gitee.com/chenmm123/microclouds</site>
//  <last-editor>cmm</last-editor>
//  <last-date>2023-11-22 15:05</last-date>
//----------------------------------------------------------------*/

namespace MicroCloud.Api.Authorization.Areas.Admin.Controllers.Auth
{
    #region "用户功能"
    /// <summary>
    /// 用户功能
    /// </summary>
    [Description("用户功能")]
    [ModuleInfo(Code = "UserFunction", Name = "用户功能", OrderNo = 7, Position = "Auth", PositionName = "授权模块")]
    public class UserFunctionController : AdminControllerBase
    {
        #region "属性"
        #region "获取 功能权限管理器"
        /// <summary>
        /// 获取 功能权限管理器
        /// </summary>
        protected FunctionAuthManager FunctionAuthManager => ServiceProvider.GetService<FunctionAuthManager>();
        #endregion
        #region "获取 用户管理器"
        /// <summary>
        /// 获取 用户管理器
        /// </summary>
        protected UserManager<User> UserManager => ServiceProvider.GetService<UserManager<User>>();
        #endregion

        #region "获取 权限服务"
        /// <summary>
        /// 获取 权限服务
        /// </summary>
        protected IAuthorityService AuthorityService => ServiceProvider.GetService<IAuthorityService>();
        #endregion

        #endregion

        #region "Action方法"
        #region "读取用户"
        /// <summary>
        /// 读取用户
        /// </summary>
        /// <param name="request">分页请求</param>
        /// <returns>用户信息</returns>
        [HttpPost]
        [ModuleInfo]
        [Description("读取用户")]
        public virtual PageData<UserOutputDtoSimple> ReadUsers(PageRequest request)
        {
            Check.NotNull(request, nameof(request));
            IFunction function = this.GetExecuteFunction();

            request.FilterGroup.Rules.Add(new FilterRule("IsLocked", false, FilterOperate.Equal));
            Expression<Func<User, bool>> predicate = FilterService.GetExpression<User>(request.FilterGroup);

            //var page = UserManager.Users.ToPage<User, UserOutputDtoSimple>(predicate, request.PageCondition);
            var page = CacheService.ToPageCache<User, UserOutputDtoSimple>(UserManager.Users, predicate, request.PageCondition, function);
            return page.ToPageData();
        }
        #endregion
        #region "读取用户功能"
        /// <summary>
        /// 读取用户功能
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="request">分页请求</param>
        /// <returns>用户功能信息</returns>
        [HttpPost]
        [ModuleInfo]
        [DependOnFunction(nameof(ReadUsers))]
        [Description("读取功能")]
        public virtual PageData<FunctionOutputDtoSimple> ReadFunctions(long userId, [FromBody] PageRequest request)
        {
            if (userId == 0)
            {
                return new PageData<FunctionOutputDtoSimple>();
            }

            // 未考虑超级管理员逻辑，暂注释备查。
            //long[] moduleIds = FunctionAuthManager.GetUserWithRoleModuleIds(userId);
            //var functionIds = FunctionAuthManager.ModuleFunctions.Where(m => moduleIds.Contains(m.ModuleId)).Select(m => m.FunctionId).Distinct().ToArray();
            //if (functionIds.Length == 0)
            //{
            //    return new PageData<FunctionOutputDtoSimple>();
            //}
            var dependOnFunctionIds = AuthorityService.GetAuthorizedInfosByUser(userId.ToString()).Select(x => x.DependOnFunctions.Select(fs => fs.Id).ToList()).ToArray();
            var functionIds = new List<long>();
            foreach (var dependOnFunctionId in dependOnFunctionIds)
            {
                functionIds = functionIds.Union(dependOnFunctionId.Distinct()).ToList();
            }
            if (functionIds.Count == 0)
            {
                return new PageData<FunctionOutputDtoSimple>();
            }

            Check.NotNull(request, nameof(request));
            IFunction function = this.GetExecuteFunction();

            Expression<Func<Function, bool>> funcExp = FilterService.GetExpression<Function>(request.FilterGroup);
            funcExp = funcExp.And(m => functionIds.Contains(m.Id));
            request.AddDefaultSortCondition(new SortCondition("Area"), new SortCondition("Controller"));
            //if (request.PageCondition.SortConditions.Length == 0)
            //{
            //    request.PageCondition.SortConditions = new[] { new SortCondition("Area"), new SortCondition("Controller") };
            //}

            //var page = FunctionAuthManager.Functions.ToPage<Function, FunctionOutputDtoSimple>(funcExp, request.PageCondition);
            var page = CacheService.ToPageCache<Function, FunctionOutputDtoSimple>(FunctionAuthManager.Functions, funcExp, request.PageCondition, function);
            return page.ToPageData();
        }
        #endregion

        #endregion

    }
    #endregion

}
